﻿using System;
using System.Collections.Concurrent;
using System.Linq;
using RpcClient.Interface;
using RpcHelper;
using RpcHelper.TaskTools;
using RpcModular.Config;
using RpcModular.Model;
using RpcModularModel.Visit;
using RpcModularModel.Visit.Model;

namespace RpcModular.Service
{
    internal class VisitService
    {
        private static string _TaskId;
        private static readonly ConcurrentDictionary<string, DictateVisit> _Census = new ConcurrentDictionary<string, DictateVisit>();
        static VisitService()
        {
            VisitConfig config = ModularConfig.GetVisitConfig();
            Config = config;
            RpcClient.RpcClient.Config.AddRefreshEvent(_Refresh);
            if (config.IsEnable)
            {
                _TaskId = TaskManage.AddTask(new TaskHelper("上传访问统计", new TimeSpan(0, 0, Config.Interval), _UpCensus));
            }
        }
        public static Action<bool> StatusRefresh;

        private static void _Enable()
        {
            _TaskId = TaskManage.AddTask(new TaskHelper("上传访问统计", new TimeSpan(0, 0, Config.Interval), _UpCensus));
            StatusRefresh(Config.IsEnable);
        }
        private static void _Stop()
        {
            if (_TaskId != null)
            {
                TaskManage.RemoveTask(_TaskId);
                _TaskId = null;
            }
            StatusRefresh(Config.IsEnable);
        }
        private static void _Refresh(IConfigServer config, string name)
        {
            if (name == string.Empty || name.StartsWith("rpcassembly:Visit"))
            {
                VisitConfig visit = ModularConfig.GetVisitConfig();
                if (visit.IsEnable != Config.IsEnable)
                {
                    Config = visit;
                    if (visit.IsEnable)
                    {
                        _Enable();
                    }
                    else
                    {
                        _Stop();
                    }
                }
                else if (visit.IsEnable && visit.Interval != Config.Interval)
                {
                    Config = visit;
                    TaskManage.SetInterval(_TaskId, new TimeSpan(0, 0, visit.Interval));
                }
            }
        }
        public static VisitConfig Config
        {
            get;
            private set;
        }
        private static void _UpCensus()
        {
            if (_Census.IsEmpty)
            {
                return;
            }
            string[] keys = _Census.Keys.ToArray();
            RpcDictateVisit[] list = keys.Convert(a => _Census[a].Sync(Config.Interval));
            if (list.Length > 0)
            {
                new AddVisitLog
                {
                    Logs = list
                }.Send();
            }
        }
        private static DictateVisit _Get(string dictate)
        {
            if (_Census.TryGetValue(dictate, out DictateVisit visit))
            {
                return visit;
            }
            visit = new DictateVisit(dictate);
            return _Census.GetOrAdd(dictate, visit);
        }
        public static void Success(string dictate)
        {
            DictateVisit obj = _Get(dictate);
            obj.Success();
        }
        public static void Failure(string dictate)
        {
            DictateVisit obj = _Get(dictate);
            obj.Failure();
        }
        public static void Regs(RpcVisit[] list)
        {
            new RegVisitNode
            {
                Visits = list,
            }.Send();
        }
        public static void Reg(RpcVisit visit)
        {
            new AddVisitNode
            {
                Visit = visit,
            }.Send();
        }
    }
}
